<?php
/**
 * Created by PhpStorm.
 * User: SJAVA015
 * Date: 2018-05-08
 * Time: 9:24
 */

namespace app\home\controller;

//use think\Collection;
use think\Controller;
use think\Db;


use app\home\model\Gn_supplier_game as MGn;
use app\home\model\Gn_supplier_user as Usu;
use think\Request;
class Xx extends Controller
{

    public function supplier(){
        if(!session('name')){
            $this->error("你没有登陆!","login");
            exit();
        }
        $name=session('name');

        $user = new MGn();
        $ziduan='';
        $sort='';
        $paixu='';
        $userPx=array('data_sort'=>'create_time','pay_sort'=>'pay','adrate_sort'=>'adrate');
        if (isset($_GET['data_sort'])||isset($_GET['pay_sort'])||isset($_GET['adrate_sort'])) {
            foreach ($userPx as $k => $v) {
                if (isset($_GET[$k])) {
                    if ($_GET[$k] == 'desc') {
                        $ziduan = $v;
                        $sort = 'desc';
                        $paixu='0';
//                      $this->assign($v, $paixu);
                    } else if ($_GET[$k] == 'asc') {
                        $ziduan = $v;
                        $sort = 'asc';
                        $paixu='1';
//                      $this->assign($v, $paixu);
                    }
                    session('paixu',$paixu);
                    session('ziduan',$ziduan);
                    session('sort',$sort);
                    $ziduan= $_SESSION['think']['ziduan'];
                    $sort= $_SESSION['think']['sort'];
                    $paixu=$_SESSION['think']['paixu'];
                    $this->assign($ziduan,$paixu);
                }else{
                    $this->assign($v, '');
                }
            }
        } else {//默认使用时间正序

            if (isset($_SESSION['think']['paixu'])||isset($_SESSION['think']['ziduan'])||isset($_SESSION['think']['sort'])){
                $ziduan= $_SESSION['think']['ziduan'];
                $sort= $_SESSION['think']['sort'];
                $paixu=$_SESSION['think']['paixu'];
                $this->assign('create_time', '');
                $this->assign('pay', '');
                $this->assign('adrate', '');
                $this->assign($ziduan,$paixu);
            }else{

                $this->assign('create_time', '');
                $this->assign('pay', '');
                $this->assign('adrate', '');
                $ziduan = 'create_time';
                $sort = 'asc';
            }


        }






//        if(isset($_POST['initial'])||isset($_POST['over'])){
        $aaa=-1;
        $bbb=9999999999;
        if (isset($_POST['initial'])&&isset($_POST['over'])){
            if ($_POST['initial']!=''&&$_POST['over']==''){
                $aaa= strtotime($_POST['initial']);
                session('aaa',$aaa);
                session('bbb',null);
            }elseif ($_POST['over']!=''&&$_POST['initial']=='') {
                $bbb = strtotime($_POST['over']);
                $bbb += 86400;                      //右边时间加24小时
                session('bbb',$bbb);
                session('aaa',null);
            }elseif ($_POST['over']!=''&&$_POST['initial']!=''){
                $aaa= strtotime($_POST['initial']);
                session('aaa',$aaa);
                $bbb = strtotime($_POST['over']);
                $bbb += 86400;                      //右边时间加24小时
                session('bbb',$bbb);
            }else {
                session('bbb', null);
                session('aaa', null);
            }

        }
        if ($aaa>$bbb){
            $this->error('亲！右边日期需要比左边大或相同');
        }
        if (isset($_SESSION['think']['aaa'])){
            $aaa=$_SESSION['think']['aaa'];
            $this->assign('aaa',$aaa);
        }else{
            $this->assign('aaa','');
        }

        if (isset($_SESSION['think']['bbb'])){
            $bbb=$_SESSION['think']['bbb'];
            $this->assign('bbb',$bbb);
        }else{
            $this->assign('bbb','');
        }



        $list = MGn::where('supplier',session('id'))->where('create_time',['>',$aaa],['<',$bbb],'and')->order([$ziduan=>$sort])->paginate(10);
        $list_sl=$user->where('supplier',session('id'))->where('create_time',['>',$aaa],['<',$bbb],'and')->select();
        $list_ss=$list_sl->toArray();
        $pages = $list->render();
        $this->assign("pages",$pages);

        $pay=0;
        $adrate=0;
        foreach ($list_ss as $k=>$v){
            $pay+=$v['pay'];
            $adrate+=$v['adrate'];
//            dump($v['adrate']);
        }
        $this->assign('name',$name);
        $this->assign('pays',$pay);
        $this->assign('adrates',$adrate);
        $this->assign('vo', $list);
//        $this->assign("vo",$users);
        return $this->fetch();

//        return view();
    }
    /*
     * 登录界面
     */
    function login(){
        return view("login");
    }
    /*
     * 退出登录
     */
    function outlogin(){
        session_start();
        session('name',null);
        $this->success("退出成功",'Xx/login');
    }
    /*
     * 检查登录信息
     */
    function jy_login(){


//        $this->success("成功");
//        $this->validate($data,[
//            'captcha|验证码'=>'require|captcha'
//        ]);

        $name =$_POST['name'];

        if ($name=="")  $this->error("请填写账号",'login');
        $pass = $_POST['pass'];
        $user = new MGn();
        $user2 = new Usu();
        $list= $user2->where("username",$name)->select();
//        $list= $user->where("id","1")->select();
        $res = $list->toArray();

        if (empty($res)){

            $this->error("该用户名不存在",'login');
        }else{
            $id=$res[0]['id'];

        }

        if (md5($pass)!=$res[0]['password']){
            $this->error("密码不正确");
        }else if(request()->isPost()){
            $data = input('post.');
            if(!captcha_check($data['verifyCode'])) {
                $this->error("请看清楚验证码，点击图片可切换");
            }else{

                session('name',$name);   //注册一个SESSION变量
                session('id',$id);   //存入改用户id
                $this->success("登陆成功",'supplier');
            }
        }
    }
}